package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TVipRights;
import com.quwan.manager.service.IVipRightsService;
import com.quwan.mapper.TVipRightsMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author laozhang
 * @date 2022/4/27 18:47
 */
@Service
@RequiredArgsConstructor
public class VipRightsServiceImpl implements IVipRightsService {
    @Autowired
    private TVipRightsMapper vipRightsMapper;
    private final OssConfig ossConfig;

    @Override
    public PageResult<TVipRights> getTVipRightsListPage(int page, int size, String name) {
        LambdaQueryWrapperX<TVipRights> wrapper = new LambdaQueryWrapperX<TVipRights>()
                .orderByDesc(TVipRights::getAddTime);
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(TVipRights::getRights, name);
        }

        Page<TVipRights> vipRightsPage = vipRightsMapper.selectPage(new Page<>(page, size), wrapper);
        List<TVipRights> reslist = vipRightsPage.getRecords();
        PageResult<TVipRights> result = new PageResult<TVipRights>()
                .setCurrent(vipRightsPage.getCurrent())
                .setPages(vipRightsPage.getPages())
                .setSize(vipRightsPage.getSize())
                .setTotal(vipRightsPage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<TVipRights> getVipRightsList(String name) {
        LambdaQueryWrapperX<TVipRights> wrapper = new LambdaQueryWrapperX<TVipRights>()
                .orderByDesc(TVipRights::getAddTime);
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(TVipRights::getRights, name);
        }
        return vipRightsMapper.selectList(wrapper).stream().map(m -> m.setIcon(StringUtils.isBlank(m.getIcon()) ? "" : ossConfig.getHostname() + "/" + m.getIcon())).collect(Collectors.toList());
    }

    @Override
    public boolean addTVipRights(TVipRights vipRights) {
        return vipRightsMapper.insert(vipRights) == 1;
    }

    @Override
    public boolean updateTVipRights(TVipRights vipRights) {
        return vipRightsMapper.updateById(vipRights) == 1;
    }

    @Override
    public TVipRights detailTVipRights(Integer id) {
        return vipRightsMapper.selectById(id);
    }

    @Override
    public boolean delTVipRights(Integer id) {
        return vipRightsMapper.deleteById(id) == 1;
    }
}
